package com.sangfor.ssl.service.line;

import android.os.Handler;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.service.line.LineSelectManager;
import com.sangfor.ssl.service.netmonitor.NetworkBean;
import com.sangfor.ssl.service.netmonitor.NetworkMonitor;
import java.util.Arrays;

/* loaded from: classes2.dex */
public class LineSelectionTask {
    private static final String TAG = "LineSelectionTask";
    private final Callback mCallback;
    private boolean mCanceled;
    private final Handler mHandler;
    private final String mInputUrl;
    private boolean mLineChanged;
    private LineResult mLineResult;
    private long mNativeSelector;
    private final NetworkBean mNetworkBean;
    private Thread mThread;
    private LineSelectManager.UserTaskInfo mUserTaskInfo;
    private int mErrorCode = 0;
    private final Runnable mCallbackRunnable = new Runnable() { // from class: com.sangfor.ssl.service.line.LineSelectionTask.1
        @Override // java.lang.Runnable
        public void run() {
            if (LineSelectionTask.this.mErrorCode != 0) {
                LineSelectionTask.this.mCallback.onSelectLineFailed(LineSelectionTask.this.mErrorCode, LineSelectionTask.this.mUserTaskInfo);
            } else {
                LineSelectionTask.this.mCallback.onSelectLineSuccess(LineSelectionTask.this.mLineResult, LineSelectionTask.this.mLineChanged, LineSelectionTask.this.mUserTaskInfo);
            }
        }
    };

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes2.dex */
    public interface Callback {
        void onSelectLineFailed(int i, LineSelectManager.UserTaskInfo userTaskInfo);

        void onSelectLineSuccess(LineResult lineResult, boolean z, LineSelectManager.UserTaskInfo userTaskInfo);
    }

    /* loaded from: classes2.dex */
    public static class ErrorCode {
        public static final int ADDR_FORMAT_ERROR = -4;
        public static final int NAME_RESOLVING_FAILED = -6;
        public static final int NONE = 0;
        public static final int NOT_ADDR = -3;
        public static final int NOT_CONN = -2;
        public static final int OUT_OF_MEMORY = -5;
        public static final int URL_EMPTY = -1;
    }

    /* loaded from: classes2.dex */
    public enum TaskType {
        CONNECT,
        LOGIN_FAILED,
        NETWORK_CHANGE,
        CONNECT_FAILED,
        OTHER
    }

    private native void cancelSelectorNative(long j);

    private native long createSelectorNative(String str);

    private native void destroySelectorNative(long j);

    private native boolean performLineSelectionNative(long j);

    public LineSelectionTask(LineSelectManager.UserTaskInfo userTaskInfo, Callback callback, NetworkBean networkBean) {
        if (userTaskInfo == null || callback == null || networkBean == null) {
            throw new IllegalArgumentException("callback cannot be empty/null");
        }
        this.mUserTaskInfo = userTaskInfo;
        this.mInputUrl = userTaskInfo.inputUrl;
        this.mHandler = userTaskInfo.handler;
        this.mCallback = callback;
        this.mNetworkBean = networkBean;
    }

    public synchronized void start() {
        if (this.mThread != null) {
            throw new IllegalStateException("cannot start LineSelectionTask twice");
        }
        Thread thread = new Thread(new Runnable() { // from class: com.sangfor.ssl.service.line.LineSelectionTask.2
            @Override // java.lang.Runnable
            public void run() {
                LineSelectionTask.this.run();
            }
        });
        this.mThread = thread;
        thread.start();
    }

    public synchronized void cancel() {
        this.mCanceled = true;
        Handler handler = this.mHandler;
        if (handler != null) {
            handler.removeCallbacks(this.mCallbackRunnable);
        }
        long j = this.mNativeSelector;
        if (j != 0) {
            cancelSelectorNative(j);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void run() {
        synchronized (this) {
            if (this.mCanceled) {
                return;
            }
            long createSelectorNative = createSelectorNative(this.mInputUrl);
            this.mNativeSelector = createSelectorNative;
            if (createSelectorNative == 0) {
                dispatchSelectLineFailed(-5);
                return;
            }
            try {
                if (!performLineSelectionNative(createSelectorNative)) {
                    Log.error(TAG, "native callback failed");
                    dispatchSelectLineFailed(-5);
                }
                synchronized (this) {
                    destroySelectorNative(this.mNativeSelector);
                    this.mNativeSelector = 0L;
                }
            } catch (Throwable th) {
                synchronized (this) {
                    destroySelectorNative(this.mNativeSelector);
                    this.mNativeSelector = 0L;
                    throw th;
                }
            }
        }
    }

    private void dispatchSelectLineFailed(int i) {
        Log.error(TAG, "dispatchSelectLineFailed() called with: code = [" + i + "]");
        synchronized (this) {
            if (this.mCanceled) {
                return;
            }
            Handler handler = this.mHandler;
            if (handler == null) {
                this.mCallback.onSelectLineFailed(i, this.mUserTaskInfo);
                return;
            }
            this.mErrorCode = i;
            handler.post(this.mCallbackRunnable);
        }
    }

    private void dispatchSelectLineSuccess(String str, String str2, int i, String[] strArr) {
        Log.debug(TAG, "dispatchSelectLineSuccess() called with: selectedUrl = [" + str + "], ipUrl = [" + str2 + "], type = [" + i + "], candidates = [" + Arrays.toString(strArr) + "]");
        boolean equals = TextUtils.equals(str, NetworkMonitor.getInstance().getCacheSelectLine(this.mNetworkBean.networkType)) ^ true;
        LineResult lineResult = new LineResult(str, i, str2, strArr);
        if (i == 2) {
            NetworkMonitor.getInstance().updateCacheNetworkInfo(this.mInputUrl, lineResult, this.mNetworkBean);
        }
        synchronized (this) {
            if (this.mCanceled) {
                return;
            }
            Handler handler = this.mHandler;
            if (handler == null) {
                this.mCallback.onSelectLineSuccess(lineResult, equals, this.mUserTaskInfo);
                return;
            }
            this.mErrorCode = 0;
            this.mLineResult = lineResult;
            this.mLineChanged = equals;
            handler.post(this.mCallbackRunnable);
        }
    }
}
